package cc.rengu.igas.channel.upac.biz.impl;

import cc.rengu.igas.channel.upac.common.constant.UpacServiceConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.facade.AuthenticationService;
import cc.rengu.igas.channel.upac.facade.request.*;
import cc.rengu.igas.channel.upac.facade.response.*;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class AuthenticationServiceImpl implements AuthenticationService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<RealNameAuthResponse> realNameAuth(RealNameAuthRequest realNameAuthRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<RealNameAuthResponse> result = new Result<>();
        RealNameAuthResponse realNameAuthResponse = new RealNameAuthResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(realNameAuthRequest,
                    UpacServiceConstant.REALNAME_AUTH_SERVICE, RealNameAuthResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((RealNameAuthResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("实名验证失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                realNameAuthResponse.setRespDesc(((BizException) e).getCode());
                realNameAuthResponse.setRespDesc(e.getMessage());
            } else {
                realNameAuthResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                realNameAuthResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<OpenAcctAuthResponse> openAcctAuth(OpenAcctAuthRequest openAcctAuthRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<OpenAcctAuthResponse> result = new Result<>();
        OpenAcctAuthResponse openAcctAuthResponse = new OpenAcctAuthResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(openAcctAuthRequest,
                    UpacServiceConstant.OPEN_ACCT_AUTH_SERVICE, OpenAcctAuthResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((OpenAcctAuthResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("开户验证失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                openAcctAuthResponse.setRespDesc(((BizException) e).getCode());
                openAcctAuthResponse.setRespDesc(e.getMessage());
            } else {
                openAcctAuthResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                openAcctAuthResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UnifiedOrgQueryResponse> unifiedOrgQuery(UnifiedOrgQueryRequest unifiedOrgQueryRequest) {
        Result<UnifiedOrgQueryResponse> result = new Result<UnifiedOrgQueryResponse>();
        UnifiedOrgQueryResponse unifiedOrgQueryResponse = new UnifiedOrgQueryResponse();
        try {
            DubboResult dubboResult = new DubboServiceImpl().callAtomService(unifiedOrgQueryRequest,
                    UpacServiceConstant.UNIFIED_ORG_QUERY_SERVICE, UnifiedOrgQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UnifiedOrgQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("开户验证查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                unifiedOrgQueryResponse.setRespDesc(((BizException) e).getCode());
                unifiedOrgQueryResponse.setRespDesc(e.getMessage());
            } else {
                unifiedOrgQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                unifiedOrgQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
